<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2018/11/27
 * Time: 5:16 PM
 */

namespace frontend\controllers;


use common\models\Food;
use yii\db\Query;
use yii\web\Controller;
use yii\web\Response;

class FoodController extends Controller
{

    public $layout = 'layout';

    public function actionIndex()
    {

        $model = new Food();
        $now = strtotime(date('Y-m-d',time()));
        $records = (new Query())->select('id,content,created_at')
                                ->from('food')
                                ->where(['between','created_at',$now,$now+3600*24])
                                ->all();
        return $this->render('index', [
            'model' => $model,
            'records'=>$records
        ]);
    }

    public function actionDelete($id)
    {
        \Yii::$app->response->format = Response::FORMAT_JSON;
        if (Food::findOne($id)->delete()){
            return ['err'=>0,'msg'=>""];
        }

    }
    public function actionSave(){
        $model = new Food();
        if (\Yii::$app->request->isPost && $model->load(\Yii::$app->request->post())) {
            $err = 0;
            $model->created_at = time();
            if ($model->content!=''&&$model->save(false)){
                $this->redirect(['food/index']);
                \Yii::$app->end();
            }else{
                $err = "1";
            }
        }
        $this->redirect(['food/index']);
        \Yii::$app->end();
    }
}